package com.netease.xone.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;

import com.netease.image.ImageType;
import com.netease.image.util.ImageUtil;
import com.netease.xone.R;
import com.netease.xone.app.XoneApp;
import com.netease.xone.widget.LoadingImageView;


public class ProfileView extends RelativeLayout {
    private LoadingImageView userFace;
    private ImageView userType;
    
    private static final int V_USER_ATTR = 6;
    private static final float density = XoneApp.getAppInstance().getResources().getDisplayMetrics().density;
    private static final int size = XoneApp.getAppInstance().getResources().getDimensionPixelSize(R.dimen.common_avatar_size);
    private static final int roundSize = XoneApp.getAppInstance().getResources().getDimensionPixelSize(R.dimen.common_avatar_rounder);
    private static final Bitmap tinyAvatar = ImageUtil.getRoundedCornerBitmap(ImageUtil.getBitmap(XoneApp.getAppInstance().getResources(),R.drawable.avatar_user_default_50,size,size), roundSize);
    public ProfileView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    
    private void init() {
       int vResId = R.drawable.icon_vip;
       int coverResId = R.drawable.border_black_double;
        
        userFace = new LoadingImageView(getContext());
        userFace.setImageBitmap(tinyAvatar);
        userFace.setScaleType(ScaleType.FIT_XY);
        LayoutParams lp = new LayoutParams(size, size);
        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        lp.bottomMargin = (int) (5 * getContext().getResources().getDisplayMetrics().density);
        lp.rightMargin = (int) (5 * getContext().getResources().getDisplayMetrics().density);
        this.addView(userFace, lp);
        
        ImageView cover = new ImageView(getContext());
        cover.setImageResource(coverResId);
        cover.setScaleType(ScaleType.FIT_XY);
        lp = new LayoutParams(size, size);
        lp.bottomMargin = (int) (5 * getContext().getResources().getDisplayMetrics().density);
        lp.rightMargin = (int) (5 * getContext().getResources().getDisplayMetrics().density);
        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        this.addView(cover,lp);
        
        userType = new ImageView(getContext());
        userType.setVisibility(View.GONE);
        userType.setImageResource(vResId);
        lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        this.addView(userType, lp);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY));
    }

    public void setImageUrl(int userAttr, String url) {
        if (userAttr != V_USER_ATTR) {
            userType.setVisibility(View.GONE);
        } else {
            userType.setVisibility(View.VISIBLE);
        }
        if (userFace.getTag() != null && userFace.getTag().equals(url)) {
            return;
        }
        userFace.setImageBitmap(tinyAvatar);
        userFace.setTag(null);
        if (!TextUtils.isEmpty(url)) {
            userFace.setServerClipSize(size, size);
            userFace.setLoadingImage(url,ImageType.RoundMemCache,roundSize);
            userFace.setTag(url);
        }
    }
}
